/****************************************************
	文件：MementoPattern.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/10 12:18:8
	功能：备忘录模式
*****************************************************/

using UnityEngine;

namespace DesignPatterns
{
    public class MementoPattern : MonoBehaviour
    {
        private void Start()
        {
            //备忘录模式：
            //常用于数据存储
            Player player = new Player(new PlayerData
            {
                Money = 1000,
                Hp = 100,
                Atk = 15,
                Def = 6,
            });
            Debug.LogFormat("初始数据：\n{0}", player.PlayerData.ToString());
            SaveContainer saveContainer = new SaveContainer();
            saveContainer.PlayerDataMemento = player.CreateMemento();//存储
            Debug.Log("存档成功");
            player.PlayerData.Money -= 200;
            player.PlayerData.Hp -= 40;
            player.PlayerData.Atk += 5;
            Debug.LogFormat("一段时间后，玩家属性：\n{0}", player.PlayerData.ToString());
            player.SetMemento(saveContainer.PlayerDataMemento);//读取
            Debug.Log("读取存档成功");
            Debug.LogFormat("读取存档后的数据：\n{0}", player.PlayerData.ToString());
        }

        /// <summary>
        /// 玩家
        /// </summary>
        public class Player
        {
            private PlayerData mPlayerData;
            public PlayerData PlayerData { get => mPlayerData; set => mPlayerData = value; }

            public Player(PlayerData data)
            {
                mPlayerData = data;
            }

            public PlayerDataMemento CreateMemento()
            {
                return new PlayerDataMemento(mPlayerData);
            }
            public void SetMemento(PlayerDataMemento memento)
            {
                PlayerData = new PlayerData
                {
                    Money = memento.Memento.Money,
                    Hp = memento.Memento.Hp,
                    Atk = memento.Memento.Atk,
                    Def = memento.Memento.Def,
                };
            }
        }
        /// <summary>
        /// 备忘录
        /// </summary>
        public class PlayerDataMemento
        {
            private PlayerData mMemento;
            public PlayerData Memento => mMemento;
            public PlayerDataMemento(PlayerData data)
            {
                mMemento = new PlayerData
                {
                    Money = data.Money,
                    Hp = data.Hp,
                    Atk = data.Atk,
                    Def = data.Def,
                };
            }
        }
        /// <summary>
        /// 存储容器
        /// </summary>
        public class SaveContainer
        {
            private PlayerDataMemento mPlayerDataMemento;
            public PlayerDataMemento PlayerDataMemento { get => mPlayerDataMemento; set => mPlayerDataMemento = value; }
        }
        /// <summary>
        /// 玩家数据
        /// </summary>
        public class PlayerData
        {
            private float mMoney;
            public float Money { get => mMoney; set => mMoney = value; }

            private float mHp;
            public float Hp { get => mHp; set => mHp = value; }

            private float mAtk;
            public float Atk { get => mAtk; set => mAtk = value; }

            private float mDef;
            public float Def { get => mDef; set => mDef = value; }

            public override string ToString()
            {
                return string.Format("玩家数据：Money:{0}--Hp:{1}--Atk:{2}--Def:{3}", Money, Hp, Atk, Def);
            }
        }
    }
}